package com.qf.zwy.office.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.office.common.base.utils.CommonBeanutils;
import com.qf.zwy.office.common.qo.CourseQo;
import com.qf.zwy.office.common.vo.CourseVo;
import com.qf.zwy.office.entity.Course;
import com.qf.zwy.office.mapper.CourseMapper;
import com.qf.zwy.office.service.CourseService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author 子未语
 */
@Service
public class CourseServiceImpl implements CourseService {
    @Resource
    private CourseMapper courseMapper;

    @Override
    public IPage<CourseVo> selectAll(CourseQo courseQo, Integer size, Integer page) {
        Page<Course> coursePage = new Page<>(size, page);
        LambdaQueryWrapper<Course> wrapper = new LambdaQueryWrapper<>();
        if (!ObjectUtils.isEmpty(courseQo.getCourseType())) {
            wrapper.eq(Course::getCourseType, courseQo.getCourseType());
        }
        if (!ObjectUtils.isEmpty(courseQo.getReleaseTime())) {
            wrapper.between(Course::getCourseType, courseQo.getReleaseTime(), new Date());
        }
        if (!ObjectUtils.isEmpty(courseQo.getQueryConditions()) && courseQo.getQueryConditions() != null) {
            wrapper.like(Course::getCourseName, courseQo.getQueryConditions())
                    .like(Course::getBriefintroduction, courseQo.getQueryConditions())
                    .like(Course::getClassification, courseQo.getQueryConditions())
                    .like(Course::getFrontCover, courseQo.getQueryConditions())
                    .like(Course::getRemark, courseQo.getQueryConditions())
                    .like(Course::getData, courseQo.getQueryConditions());
        }
        return CommonBeanutils.convertPageInfo(courseMapper.selectAll(coursePage, wrapper), new Page<>(), CourseVo::new);
    }
}
